<template>
  <div class="container">
    <div class="app-container">
      <el-card shadow="always">
        <el-row>
          <el-button type="primary" @click="addPermission(0, 1)">
            添加权限
          </el-button>
        </el-row>
        <!-- 表格 -->
        <el-table
          :data="list"
          style="width: 100%; margin-bottom: 20px"
          row-key="id"
          border
          default-expand-all
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <el-table-column prop="name" label="名称" sortable width="180">
          </el-table-column>
          <el-table-column prop="code" label="标识" sortable width="180">
          </el-table-column>
          <el-table-column prop="description" label="描述"></el-table-column>
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button type="text" @click="addPermission(row.id, 2)"
                >添加</el-button
              >
              <el-button type="text" @click="updataPermission(row.id)"
                >编辑</el-button
              >
              <el-button type="text" @click="delPermission(row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 新增弹层 -->
    <el-dialog :title="`${show}权限点`" :visible.sync="isDislog" width="40%">
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="活动名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="开启" prop="enVisible">
          <el-switch
            v-model="form.enVisible"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
          >
          </el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 删除弹层 -->
    <el-dialog :visible.sync="dialogVisible" width="30%">
      <span>确定要删除这条权限吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delCancel">取 消</el-button>
        <el-button type="primary" @click="delConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getPermissionList,
  addPermission,
  updataPermission,
  getPermission,
  delPermission,
} from "@/api/permission";
import { transListToTreeData } from "@/utils";
export default {
  name: "Permission",
  data() {
    return {
      list: [], // 页面渲染列表
      isDislog: false, // 新增、编辑弹层开关
      dialogVisible: false, // 删除弹层开关
      delID: "",
      form: {
        name: "", // 名称
        code: "", // 标识
        description: "", // 描述
        type: "", // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: "", // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: 0, // 默认关闭
      },
      rules: Object.freeze({
        name: [{ required: true, message: "名字不能为空", trigger: "blur" }],
        code: [
          {
            required: true,
            message: "权限标识必须为英文字符",
            trigger: "blur",
          },
        ],
      }),
    };
  },
  computed: {
    show() {
      return;
    },
  },
  created() {
    this.getPermissionList();
  },
  methods: {
    // 调用 权限 列表信息
    async getPermissionList() {
      this.list = transListToTreeData(await getPermissionList(), 0);
    },
    // 调用新增接口
    addPermission(pid, type) {
      this.form.pid = pid;
      this.form.type = type;
      this.isDislog = true;
    },
    // 调用编辑接口
    async updataPermission(id) {
      this.form = await getPermission(id);
      this.isDislog = true;
    },
    // 调用删除弹层
    delPermission(id) {
      this.dialogVisible = true;
      this.delID = id;
    },
    // 新增、编辑弹层取消按钮点击事件
    cancel() {
      // 1. 清空表单
      this.$refs.form.resetFields();
      // 2. 关闭弹层
      this.isDislog = false;
    },
    // 新增、编辑弹层取消按钮点击事件
    async confirm() {
      // 1. 校验表单，不合法结束
      this.$refs.form.validate((isOk) => {
        if (!isOk) return;
      });
      // 2. 合法调接口
      //   const {type,pid,...result} = this.form
      if (this.form.id) {
        await updataPermission(this.form);
        // 4. 成功提示
        this.$message.success("编辑权限点成功");
      } else {
        await addPermission(this.form);
        this.$message.success("新增权限点成功");
      }
      // 3.  更新结果
      this.getPermissionList();
      // 5. 关闭表单，清空表单
      this.cancel();
    },
    // 删除弹层取消按钮点击事件
    delCancel() {
      this.dialogVisible = false;
    },
    // 确认删除弹层
    async delConfirm() {
      // 1. 调接口
      await delPermission(this.delID);
      // 2. 渲染页面
      this.getPermissionList();
      // 3. 关闭弹层
      this.delCancel();
    },
  },
};
</script>
